/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.field;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.field.FieldGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.Initializer;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.StringLiteral;

public class PropNamesFieldGenerator extends FieldGenerator {

  private List<String> fields;
  private final String fieldName;

//  public PropNamesFieldGenerator(List<IGspCommonField> fields, String fieldName) {
//    this.fields = fields.stream().map(item -> item.getLabelID()).collect(Collectors.toList());
//    this.fieldName = fieldName;
//  }

  public PropNamesFieldGenerator(List<MediatePropertyInfo> fields, String fieldName){
    this.fields = fields.stream().filter(item->!item.getExtendInfos().containsKey("AssoUdtAtrribute")).map(item -> item.getPropertyName()).collect(Collectors.toList());
    this.fieldName = fieldName;
  }

  @Override
  protected String getFieldName() {
    return fieldName;
  }

  @Override
  protected TypeInfo getFieldType() {
    addImport(ArrayList.class.getTypeName());
    TypeInfo typeInfo = new TypeInfo(ArrayList.class.getTypeName());
    typeInfo.setIsParameterizedType(true);
    typeInfo.getArguments().add(new TypeInfo(String.class.getTypeName()));
    return typeInfo;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(ModifierKeyword.PRIVATE_KEYWORD);
    list.add(ModifierKeyword.STATIC_KEYWORD);
    return list;
  }

  @Override
  protected Expression buildGetMethodBody() {
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(new TypeInfo(ArrayList.class).getType(ast));
    classInstanceCreation.arguments()
        .add(ast.newNumberLiteral(Integer.toString(fields.size())));
    AnonymousClassDeclaration anonymousClassDeclaration = ast.newAnonymousClassDeclaration();
    setAddItemsStatement(anonymousClassDeclaration);
    classInstanceCreation.setAnonymousClassDeclaration(anonymousClassDeclaration);
    return classInstanceCreation;
  }

  private void setAddItemsStatement(AnonymousClassDeclaration anonymousClassDeclaration) {
    Block block = ast.newBlock();
    for (String field : fields) {
      MethodInvocation methodInvocation = ast.newMethodInvocation();
      methodInvocation.setName(ast.newSimpleName("add"));
      StringLiteral stringLiteral = ast.newStringLiteral();
      stringLiteral.setLiteralValue(field);
      methodInvocation.arguments().add(stringLiteral);

      ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
      block.statements().add(expressionStatement);
    }
    Initializer initializer = ast.newInitializer();
    initializer.setBody(block);
    anonymousClassDeclaration.bodyDeclarations().add(initializer);
  }
}
